<?php
if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/**
 * -----------------------------------------------------
 *
 * manage user
 *
 * @package     manage
 * @subpackage  
 * @category    Category - 分类管理
 * @author      ChenFu
 * @since       2015-6-17 version 1.0
 * -----------------------------------------------------    
 */
class Category extends CI_Controller{
	public function __construct(){
		parent::__construct();
		$this->load->helper(array('form', 'url'));
	  	$this->load->library('form_validation'); // 表单验证类
		$this->load->model('category_model');    // 加载分类模型
		$this->load->library('my_md5');
		check_login(); // 检查是否登录
	}

	/**
	 * 
	 * @return [type] [list]
	 */
	public function index()
	{
		$category = $this->category_model->to_list(0,0,true); // 分类数据
		$this->load->view('admin/category/index',compact("category"));
	}

	/**
	 * 增加分类
	 */
	public function insert()
	{
		$category = $this->category_model->to_options(0,0); // 分类数据
		if ($this->form_validation->run('category') == FALSE){
		    $this->load->view('admin/category/create',compact("category"));
		}else{
			if(!empty($_POST['cate_name'])){
				$arr = $this->input->post();
				$_insert['cate_pid']    = $arr['cate_pid'];
				$_insert['cate_name']   = $arr['cate_name'];
				$_insert['cate_sort']	= $arr['cate_sort'];
				$_insert['cate_status']	= $arr['cate_status'];
				if($this->category_model->insert($_insert)){
					$this->sys_message->skip('增加分类成功',base_url('manage/category'));
				}else{
					$this->sys_message->show_error('增加用户失败,请重新添加！');
				}
			
			}
		}
	}

	/**
	 * 编辑分类信息
	 */
	public function update()
	{
		// 默认值
		$catearr = $this->category_model->get_category_by_cid($this->input->get('id'));
		// 分类数据
		$category = $this->category_model->to_options(0,0,$catearr['cate_id']); // 分类数据


		if ($this->form_validation->run('category') == FALSE){
		    $this->load->view('admin/category/update',compact("catearr","category"));
		}else{
			if(!empty($_POST['cate_name'])){
				$arr = $this->input->post();
				$_update['cate_pid']    = $arr['cate_pid'];
				$_update['cate_name']   = $arr['cate_name'];
				$_update['cate_sort']	= $arr['cate_sort'];
				$_update['cate_status']	= $arr['cate_status'];
				if($this->category_model->update($_update,$this->input->get('id'))){
					$this->sys_message->skip('编辑成功',site_url('manage/category'));
					return ;
				}else{
					$this->sys_message->show_error('编辑失败,请重新编辑！');
				}
			}
		}
	}

	/**
	 * 删除分类
	 */
	public function delete()
	{
		if(!empty($_GET['id']) && intval($_GET['id'])){
			$menu = $this->category_model->get_pid_date($_GET['id']);
			if(count($menu) != 0){
				die('error_1');
			}
			if($this->category_model->delete($_GET['id'])){
				die('ok');
			}else{
				die('error_2');
			}
		}else{
			die('error_3');
		}
	}
}


